<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-28
 * Time: 11:56
 * 用户预约上门取件时间
 */

namespace app\api\service\store;


use app\api\model\StoreOrder;
use app\api\model\User;
use app\api\service\Token;
use app\lib\exception\ParameterException;

class SendStoreOrderSer extends Token
{
    /**
     * 1. 获取用户信息，订单信息
     * 2. 判断订单信息
     * 3. 修改订单状态
     * 4. 返回成功
     */

    // 用户信息
    private $user;

    // 订单信息
    private $order;


    // 传来的数据
    private $dataArr;

    function __construct($dataArr)
    {
        $this->dataArr = $dataArr;
        $this->user = User::get(self::getCurrentUid());
        $this->order = StoreOrder::get($this->dataArr['order_id']);
    }

    // 入口方法
    public function entrance()
    {
        return $this->judgeOrder();
    }

    // 判断订单
    private function judgeOrder()
    {
        if(!$this->order) {
            throw new ParameterException([
                'msg' => [
                    'order' => '请重新选择订单'
                ]
            ]);
        }
        if($this->order['status'] < 4) {
            throw new ParameterException([
                'msg' => [
                    'order' => '订单状态错误'
                ]
            ]);
        }
        if($this->order['status'] == 6) {
            throw new ParameterException([
                'msg' => [
                    'order' => '订单已完成'
                ]
            ]);
        }
        if($this->order['user_id'] != $this->user['id']) {
            throw new ParameterException([
                'msg' => [
                    'order' => '不是您的订单'
                ]
            ]);
        }
        return $this->updateData();
    }

    // 修改订单状态并返回
    private function updateData()
    {
        $storeOrder = new StoreOrder();
        $storeOrder->save([
            'status' => 6,
            'send_time' => time()
        ],[ 'id' => $this->order['id'] ]);
        return writeJson(202, [], '完成成功！');
    }
}